using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FileMoveComponent:WorkflowComponentBase
    {
        private FileMoveInstruction _Instruction;
        private FileMoveOutput _Output;
        /// <summary>
        /// 
        /// </summary>
        public FileMoveComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.Description = "move files";
            this.ComponentIcon = Resources.FlatFileSrc_201;
            this.ComponentImage = Resources.Library;
            this.ComponentName = "Move file task";

            this._Instruction = new FileMoveInstruction(this.ComponentID, Guid.Empty, null, string.Empty);
            this._Output = new FileMoveOutput(this.ComponentID, Guid.Empty, null);
        }

        #region props
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(FileMoveInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (FileMoveOutput) value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<string> SrcFiles
        {
            get { return this._Instruction.SrcFiles; }
            set { this._Instruction.SrcFiles = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string TgtFolder
        {
            get { return this._Instruction.TgtFolder; }
            set { this._Instruction.TgtFolder = value; }
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TargetFolderPath", this.TgtFolder);
            XmlNode srcFileRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "SourceFiles");
            if (this.SrcFiles != null && this.SrcFiles.Count > 0)
            {
                foreach (string filePath in this.SrcFiles)
                {
                    XmlNode fileNode = XmlDataUtil.AddElement(ref xDoc, srcFileRootNode, "File");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fileNode, "FilePath", filePath);
                }
            }
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileMoveComponent fileMover = base.Instantiate(xNode) as FileMoveComponent;

                fileMover.TgtFolder = xNode.Attributes.GetNamedItem("TargetFolderPath").Value;
                fileMover.SrcFiles = null;
                XmlNodeList fileNodes = xNode.SelectNodes("SourceFiles/File");
                if (fileNodes != null && fileNodes.Count > 0)
                {
                    fileMover.SrcFiles = new List<string>();
                    foreach (XmlNode fileNode in fileNodes)
                    {
                        string filePath = fileNode.Attributes.GetNamedItem("FilePath").Value;
                        fileMover.SrcFiles.Add(filePath);
                    }
                }
                return fileMover;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }
    }
}
